package com.mdd.front.service.specification.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.constant.GlobalConstant;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.goods.Goods;
import com.mdd.common.entity.goods.GoodsSpecification;
import com.mdd.common.entity.salesman.SalesmanGoogs;
import com.mdd.common.entity.salesman.UserSalesman;
import com.mdd.common.mapper.goods.GoodsMapper;
import com.mdd.common.mapper.googs.SalesmanGoogsMapper;
import com.mdd.common.mapper.salesman.UserSalesmanMapper;
import com.mdd.common.mapper.specification.GoodsSpecificationMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.googs.ISalesmanGoogsService;
import com.mdd.front.service.specification.IGoodsSpecificationService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.specification.GoodsSpecificationListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 服务规格实现类
 */
@Service
public class GoodsSpecificationServiceImpl implements IGoodsSpecificationService {

    @Resource
    GoodsSpecificationMapper goodsSpecificationMapper;

    @Resource
    GoodsMapper goodsMapper;

    @Resource
    SalesmanGoogsMapper salesmanGoogsMapper;
    @Resource
    UserSalesmanMapper userSalesmanMapper;

    /**
     *
     * @param param  第一个参数为商品规格id，第二个为是否优惠
     * @return
     */
    public Goods getGoodsBySpecificationId(Long... param){
        int length = param.length;
        Long specificationId = null;
        Long isBuy = null;
        if (length > 0){
            specificationId = param[0];
        }
        if (length > 1 ){
            isBuy = param[1];
        }

        GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectById(specificationId);
        Goods goods = goodsMapper.selectById(goodsSpecification.getGoodsId());
        goods.setPrice(goodsSpecification.getPrice());
        if (Objects.nonNull(isBuy) && isBuy.equals(Long.valueOf(GlobalConstant.DELETE.intValue()))){
            goods.setPrice(goodsSpecification.getBuyNumber());
        }
        return goods;
    }



    /**
     * 服务规格列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<GoodsSpecificationListVo>
     */
    @Override
    public PageResult<GoodsSpecificationListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();
        String goodsId = params.get("goodsId");
        BigDecimal productPrice  = null;

        QueryWrapper<GoodsSpecification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        goodsSpecificationMapper.setSearch(queryWrapper, params, new String[]{
                "=:goodsId@goods_id:long",
                "like:name:str",
                "=:price:str",
                "=:incrementPrice@increment_price:str",
                "=:createdBy@created_by:long",
                "=:updatedBy@updated_by:long",
                "=:remark:str",
        });

        IPage<GoodsSpecification> iPage = goodsSpecificationMapper.selectPage(new Page<>(page, limit), queryWrapper);

        // 获取是否有推销员价格
        if (!goodsId.isEmpty()){
            Integer userId = LikeFrontThreadLocal.getUserId();
            Integer salesmanId = userSalesmanMapper.selectOne(new LambdaQueryWrapper<UserSalesman>().eq(UserSalesman::getUserId, userId)).getSalesmanId();
            SalesmanGoogs salesmanGoogs = salesmanGoogsMapper.selectOne(new LambdaQueryWrapper<SalesmanGoogs>().eq(SalesmanGoogs::getSalesmanId, salesmanId).eq(SalesmanGoogs::getProductId, goodsId));
            if (Objects.nonNull(salesmanGoogs)){
                productPrice = salesmanGoogs.getProductPrice();
            }
        }

        List<GoodsSpecificationListVo> list = new LinkedList<>();
        for(GoodsSpecification item : iPage.getRecords()) {
            GoodsSpecificationListVo vo = new GoodsSpecificationListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            if(iPage.getRecords().size() == 1 && Objects.nonNull(productPrice)){
                vo.setBuyNumber(productPrice);
                vo.setPrice(productPrice);
            }
            // 此规格的商品，是否有
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }



}
